﻿using System;
using System.Collections.Generic;
using System.Net;
using JetBlack.FeedBus.Adapters;

namespace ExampleNotifier
{
    class Program
    {
        static Client client;

        static void Main(string[] args)
        {
            client = new Client();

            // Attempt to connect.
            try
            {
                // Assume the Distributor is running on the local machine.
                client.Connect(Dns.GetHostName(), 9123);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to connect to distributor: {0}", ex.Message);
                Environment.Exit(-1);
            }

            client.OnForwardedSubscription += OnForwardedSubscription;

            Console.WriteLine("Press <enter> to add notifications ...");
            Console.ReadLine();

            client.AddNotification("LSE", true);

            Console.WriteLine("Press <enter> to remove notifications ...");
            Console.ReadLine();

            client.RemoveNotification("LSE", true);

            Console.WriteLine("Press <enter> to quit.");
            Console.ReadLine();

            client.Close();
        }

        static void OnForwardedSubscription(int clientId, string feed, string topic, bool isAdd)
        {
            Console.WriteLine("Received an {0} subscription from client {1} on feed {2} topic {3}", isAdd ? "add" : "remove", clientId, feed, topic);
            if (isAdd)
            {
                client.Send(clientId, feed, topic, true, new Dictionary<string, object> { { "Now", DateTime.Now }, { "ClientId", clientId } });
            }
        }
    }
}
